//
//  ViewController.m
//  富文本
//
//  Created by iOS on 2017/10/16.
//  Copyright © 2017年 iOS. All rights reserved.
//

#import "ViewController.h"
#import "JJCreateHtml.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController () 
@property (weak, nonatomic) IBOutlet UITextView *textView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSTextAttachment *ddddddd = [[NSTextAttachment alloc] init];
    ddddddd.image = [UIImage imageNamed:@"升级"];
    ddddddd.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH*760/1080.0);
    NSAttributedString *attributed = [NSAttributedString attributedStringWithAttachment:ddddddd];
    self.textView.attributedText = attributed;
}
//生成html文件
- (IBAction)createHtml:(UIButton *)sender {
    NSString *htmlString  = [self attriToStrWithAttri:self.textView.attributedText];
    JJCreateHtml *html = [[JJCreateHtml alloc] init];
    NSLog(@"路劲是 %@",[html createHtml:htmlString]);
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    //插入图片
    NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
    attachment.image = [UIImage imageNamed:@"收藏"];
    attachment.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH);
    NSAttributedString *attributed = [NSAttributedString attributedStringWithAttachment:attachment];

    NSMutableAttributedString *oldMutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
    NSRange oldRange = self.textView.selectedRange;
    [oldMutableAttrString replaceCharactersInRange:oldRange withAttributedString:attributed];
    self.textView.attributedText = oldMutableAttrString;
    self.textView.selectedRange = NSMakeRange(oldRange.location + 1, 0);
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@""]) {
        return YES;
    }
    //插入文字
    NSDictionary *stringFountDic = @{NSForegroundColorAttributeName:[UIColor redColor],
                                     NSFontAttributeName:[UIFont systemFontOfSize:15]
                                     };
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:stringFountDic];

    NSMutableAttributedString *oldMutableAttrString = [[NSMutableAttributedString alloc] initWithAttributedString:self.textView.attributedText];
    NSRange oldRange = self.textView.selectedRange;
    [oldMutableAttrString replaceCharactersInRange:oldRange withAttributedString:string];
    self.textView.attributedText = oldMutableAttrString;
    self.textView.selectedRange = NSMakeRange(oldRange.location + 1, 0);

    return NO;
}


/**
 *  富文本转html字符串
 */
- (NSString *)attriToStrWithAttri:(NSAttributedString *)attri{
    NSDictionary *tempDic = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
                              NSCharacterEncodingDocumentAttribute:[NSNumber numberWithInt:NSUTF8StringEncoding]};
    NSData *htmlData = [attri dataFromRange:NSMakeRange(0, attri.length)
                         documentAttributes:tempDic
                                      error:nil];
    return [[NSString alloc] initWithData:htmlData
                                 encoding:NSUTF8StringEncoding];
}

/**
 *  字符串转富文本
 */
- (NSAttributedString *)strToAttriWithStr:(NSString *)htmlStr{
    return [[NSAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUnicodeStringEncoding]
                                            options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                 documentAttributes:nil
                                              error:nil];
}
@end
